import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,parseToChart,propConfig} from '../apis/core-util'
import basicInfo from '../apis/basicInfo'
import {tracelistApi,createGridOrder,parseToPartGrid} from '../apis/tracelist'
var findViewTitle = (function(){
  var options = _.chain(propConfig).pick('avg_exec','total_exec','exec_count').map(function(v,k){
    return {
      key:k,
      label:v.label
    }
  }).value()
  return function(key){
    var r = _.find(options,(i) => i.key === key)
    return r?r.label:''
  }
})()
export default class MainScope extends Scope {
  constructor(props){
    super(props);
    this.viewKey = props.viewKey;
    this.nameId = props.routeParams.interactionId;
    // this.hostname = props.routeParams.hostname;
    this.hostname = props.routeParams.hostname === undefined ? undefined : decodeURIComponent(props.routeParams.hostname);
    // console.log('hostname', this.hostname)
    this.$state = {
      title:'',
      viewTitle:'',
      chart0:{
        loading:true
      },
      chart1:{
        loading:true
      },
      grid:{
        loading:true,
        getOrder:createGridOrder('timestamp',-1),
        fetch:this.fetchGrid.bind(this)
      }
    }
    this.basicInfo = basicInfo({
      id:this.nameId,
      urlName:'interaction'
    })
    this.chart0Api = coreApi({
      limit:1,
      nameId:this.nameId,
      classify:'measurementname',
      urlName:'webview'
    },TimeStore.getValue, () => {
      return {
        order:sortMap[this.viewKey],
        viewKey:this.viewKey
      }
    },() => {
      if(this.hostname){
        return {
          urlName:'webviewdomain',
          hostName:this.hostname,
          classify:'hostname'
        }
      }
    });
    if(!this.hostname){
      this.chart1Api = coreApi({
        limit:5,
        mode:'ui',
        nameId:this.nameId,
        classify:'measurementname',
        urlName:'interactionDetail',
        viewKey:'exec_count'
      },TimeStore.getValue,function (){
        return {order:sortMap[this.viewKey]}
      });
      this.gridApi = tracelistApi({
        urlName:'webViewlist',
        nameId:this.nameId
      },TimeStore.getValue,this.$state.grid.getOrder);
    }else{
      this.chart1Api = coreApi({
        limit:5,
        mode:'ui',
        nameId:this.nameId,
        classify:'hostname',
        hostName:this.hostname,
        urlName:'webviewdomain',
        viewKey:'bDTotal'
      },TimeStore.getValue,function (){
        return {order:sortMap[this.viewKey]}
      });
      this.gridApi = tracelistApi({
        urlName:'webViewlist',
        nameId:this.nameId,
        hostname:this.hostname
      },TimeStore.getValue,this.$state.grid.getOrder);
    }

  }
  fetch(){
    var _this = this
    this.chart0Api.fetch().then(function(data,params){
      _this.$state.chart0 = {
        data:parseToChart(data,params)
      }
      _this.$state.viewTitle = findViewTitle(params.viewKey)
    },function(err){
      _this.$state.chart0 = {
        error:err
      }
    },function(params){
      _this.$state.chart0 = {
        loading:true,
        params:params
      }

      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.chart1Api.fetch().then(function(data,params){
      _this.$state.chart1 = {
        data:parseToChart(data,params)
      }
    },function(err){
      _this.$state.chart1 = {
        error:err
      }
    },function(params){
      _this.$state.chart1 = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
    this.fetchGrid()
  }
  fetchGrid(){
    var _this = this
    this.gridApi.fetch().then(function gridHandle(data,params){
      _this.$state.grid = parseToPartGrid(data,params)
    },function(err){
      _this.$state.grid = {
        error:err
      }
    },function(params){
      _this.$state.grid = {
        loading:true,
        params:params
      }
      _this.emit()
    }).always(function(){
      _this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $rename_interaction(payload){
    this.$state.title = payload.name
    this.emit()
  }
  $unload(){
  }
  $changesort(payload){
    this.viewKey = payload.key;
    this.fetch()
  }
  $load(){
    var _this = this
    this.basicInfo.fetch().done(function(data){
      //TODO 当basicInfo查不到时应显示错误,可以在Router.transtition时加载basicInfo并判断
      _this.$state.title = data.result[0].alias||data.result[0].name
      _this.emit()
    })
    this.fetch();

  }
}
